/**
 * File : index.js
 * By : Minglie
 * QQ: 934031452
 * Date :2021.09.30
 * version :1.4.0
 */

(function (window) {

    window.a=function (x){
        return (x*Math.PI/180)
    }

    window.DH={
        notClean:true,
        L1:300,
        L2:400,
        a1min: a(30),
        a1max: a(90),
        a2min: a(90),
        a2max: a(150),
    }


    class MingCanvas {

        constructor(el,width,height) {


            this.canvas = document.querySelector(el);
            this.canvas.width = 1800;
            this.canvas.height = 1600;
            this.ctx =this.canvas.getContext('2d');
            this.ctx.strokeStyle = 'red';
            // 设置线条的宽度
            this.ctx.font="20px Georgia";
            this.ctx.lineWidth = 5;
            this.ctx.translate(0, 1000)
            this.ctx.scale(1, -1);
            this.ctx.fillStyle = "green";

        }

        line(x1,y1,x2,y2,color="red",lineWidth=2){
            this.ctx.beginPath();
            this.ctx.lineWidth=lineWidth;
            this.ctx.strokeStyle=color; // Green path
            this.ctx.moveTo(x1,y1);
            this.ctx.lineTo(x2,y2);

            this.ctx.stroke();

           this.ctx.fillText(`(${x1},${y1})`,x1,y1);
            this.ctx.fillText(`(${x2},${y2})`,x2,y2);

            C.drawDot(x2,y2)
        }

        arc(x,y,r,sAngle=0,eAngle=2*Math.PI,counterclockwise=false){
            this.ctx.beginPath();
            this.ctx.arc(x,y,r,sAngle,eAngle,counterclockwise);
            this.ctx.stroke();
        }

        drawDot(x,y,dotRadius=10,dotColor="red"){
            this.ctx.beginPath();
            this.ctx.arc(x,y, dotRadius, 0, 2 * Math.PI, false);
            this.ctx.fill();

           // this.ctx.fillText(`(${x},${y})`,x,y);
        }
        draw1(a1,a2,c){
            a1=a(a1);
            a2=a(a2);
            this.draw(a1,a2,c);
        }


        draw(a1,a2,c="red"){
            //console.log("input",a1,a2)

            if(a1>DH.a1max){
                a1=DH.a1max;
            }
            if(a1<DH.a1min){
                a1=DH.a1min;
            }
            if(a2<DH.a2min){
                a2=DH.a2min;
            }
            if(a2>DH.a2max){
                a2=DH.a2max;
            }
            let x1=DH.L1*Math.cos(a1)
            let y1=DH.L1*Math.sin(a1)

            let a3=a2- (Math.PI- a1);

            let x2=DH.L2*Math.cos(a3)
            let y2=DH.L2*Math.sin(a3)

            window.a1=a1;
            window.a2=a2;
            window.a3=a3;
            window.x1=x1;
            window.x2=x2;
            window.y1=y1;
            window.y2=y2;
           // console.log("a==>",x1,y1,a1*Math.PI,a2*Math.PI,a3*Math.PI)

            x2=x2+x1;
            y2=y2+y1;

            if(!DH.notClean){
                this.clear()
            }

            this.drawDot(0,0);
            this.line(0,0,x1,y1,c);
            this.line(x1,y1,x2,y2,c);

           // console.log(x1,y1,x2,y2)
        }



        clear(){
            this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
            this.ctx.clearRect(0,0,-1*this.canvas.width,this.canvas.height);
        }






        static transLate(x,y){
            let x2=x+1000;
            let y2=1000-y;
            return {x:x2,y:y2}
        }

    }

    window.MingCanvas=MingCanvas;




})(window);